package com.ifengchao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifengchao.dao.iface.DealReptileDao;
import com.ifengchao.pojo.HConfig;
import com.ifengchao.pojo.HCrawlLog;
import com.ifengchao.pojo.HMessagePlat;
import com.ifengchao.service.DealReptileService;
import com.ifengchao.util.ReptileUtil;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The type Deal reptile service.
 *
 * @Author YiMing
 * @Description:
 * @Date: Created in 下午5:10 2017/9/11
 * @Modified By
 */
@Service
public class DealReptileServiceImpl implements DealReptileService {

	private static final Logger logger = Logger.getLogger(DealReptileServiceImpl.class);

	@Autowired(required = true)
	private DealReptileDao dealReptileDao;

	private static String hyzUrl = null; //慧驿站url

	@Override
	public void getData() {
		//查询爬虫配置开关是否开启
		Map<String, String> map = dealReptileDao.querySetting();

		String flag = map.get("reptile_thread_flag");

		hyzUrl = map.get("hyzUrl");

		logger.info("开关状态是:" + flag);

		//用于存储爬虫运行时间list
		ArrayList<HCrawlLog> hCrawlLogs = new ArrayList<>();

		if ("Y".equals(flag)) {
			List<HConfig> hConfigList = dealReptileDao.queryConfig();
			for (HConfig hConfig : hConfigList) {
				if ("P00012".equals(hConfig.getPlatNo())) {
					ReptileUtil.getMessage(hCrawlLogs, hConfig,"澎湃新闻",dealReptileDao);
				} else if ("P00011".equals(hConfig.getPlatNo())) {
					ReptileUtil.getMessage(hCrawlLogs, hConfig,"36氪",dealReptileDao);
				} else if ("P00016".equals(hConfig.getPlatNo())) {
					ReptileUtil.getMessage(hCrawlLogs, hConfig,"TEDNEWS",dealReptileDao);
				} else if ("P00017".equals(hConfig.getPlatNo())) {
					ReptileUtil.getMessage(hCrawlLogs, hConfig,"网易公开课",dealReptileDao);
				}
			}

			if (hCrawlLogs.size() > 0) {
				logger.info("开始插入爬虫记录表");
				dealReptileDao.insertCrawlLog(hCrawlLogs);
			}
		} else {
			logger.info("=========爬虫配置开关没有开启========");
		}
	}

	//获取TED
	public static void getTedNewsData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {
		String site = hConfig.getSite(); //首页网址
		String platNo = hConfig.getPlatNo(); //资讯编号
		Date published = null; //文章时间

		//根据platNo去资讯表中查询来源
		String mediaSource = dealReptileDao.queryMenu(platNo); //数据来源
		if (mediaSource == null) {
			logger.info("===========没有查询到数据来源==========");
			return;
		}

		//获取首页html信息
		JSONArray result = getTedIndexHtml(site);

		List<HMessagePlat> hMessagePlats = new ArrayList<>();

		//开始遍历获取视频内容
		//outer:
		getTedContent(dealReptileDao, hConfig, published, mediaSource, result, hMessagePlats);
	}

	/**
	 * Gets wang yi data.
	 * 网易公开课
	 *
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 */
	public static void getWangYiData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		String site = hConfig.getSite(); //首页网址
		String platNo = hConfig.getPlatNo(); //资讯编号
		Date published = null; //文章时间

		//根据platNo去资讯表中查询来源
		String mediaSource = dealReptileDao.queryMenu(platNo); //数据来源
		if (mediaSource == null) {
			logger.info("===========没有查询到数据来源==========");
			return;
		}

		//获取首页html信息
		JSONArray result = getTedIndexHtml(site);

		List<HMessagePlat> hMessagePlats = new ArrayList<>();

		//开始遍历获取视频内容
		getWangYiContent(dealReptileDao, hConfig, published, mediaSource, result, hMessagePlats);
	}

	/**
	 * Gets thirty six data.
	 * 获取36氪数据
	 *
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 * @throws Exception the exception
	 */
	public static void getThirtySixData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		String site = hConfig.getSite(); //首页网址
		String secondSite = hConfig.getSecondSite(); //二级网页网址
		String platNo = hConfig.getPlatNo(); //资讯编号

		//根据platNo去资讯表中查询来源
		String mediaSource = dealReptileDao.queryMenu(platNo); //数据来源
		if (mediaSource == null) {
			logger.info("===========没有查询到数据来源==========");
			return;
		}

		long current = System.currentTimeMillis();//当前时间
		long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		long ten = zero + 10 * 60 * 60 * 1000;//今天10点零分零秒的毫秒数

		//获取36氪文章内容
		getThirtySixContent(dealReptileDao, hConfig, site, secondSite, platNo, mediaSource, current, zero, ten);

	}

	/**
	 * Gets paper data.
	 * 查询澎湃新闻当天数据
	 *
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 * @return the paper data
	 * @throws Exception the exception
	 */
	public static void getPaperData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		String site = hConfig.getSite(); //首页网址
		String secondSite = hConfig.getSecondSite(); //二级网页网址
		String platNo = hConfig.getPlatNo(); //资讯编号

		//根据platNo去资讯表中查询来源
		String mediaSource = dealReptileDao.queryMenu(platNo); //数据来源

		if (mediaSource == null) {
			logger.info("===========没有查询到数据来源==========");
			return;
		}
		//澎湃新闻
		String column = null; //栏目

		long current = System.currentTimeMillis();//当前时间
		long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		long ten = zero + 10 * 60 * 60 * 1000;//今天10点零分零秒的毫秒数

		List<Map<String, Object>> mapList = new ArrayList<>();

		int index = 1;
		//获取澎湃新闻内容
		getPaperContent(dealReptileDao, hConfig, site, secondSite, platNo, mediaSource, column, current, zero, ten, index);

		return;
	}


	//获取TED视频内容
	private static void getTedContent(DealReptileDao dealReptileDao, HConfig hConfig, Date published, String mediaSource, JSONArray result, List<HMessagePlat> hMessagePlats) throws Exception {
		String picture;
		String title;
		String video;
		String content;
		String author;
		String column;
		for (Object o : result) {
			//当条数超过2条执行插入
			if (hMessagePlats.size() >= 2) {
				dealReptileDao.insertMessage(hMessagePlats);
				return;
			}

			JSONObject o1 = (JSONObject) o;
			JSONObject iteminfo = o1.getJSONObject("iteminfo");
			picture = iteminfo.getString("imgurl"); //获取封面图片
			title = iteminfo.getString("title");//获取标题

			//判断是否不含有TED 是的话跳过
			if (!title.contains("TED")) {
				continue;
			}

			//获取关键字
			String keyword = hConfig.getKeyword();
			//判断关键字是否为null不为null默认全爬取为null的话值获取带有关键字的title文章
			if (keyword != null && !"".equals(keyword.trim())) {
				if (!title.contains(keyword)) {
					continue;
				}
			}

			String url = iteminfo.getString("url");
			System.out.println(picture + ":===" + title + ":===" + url);

			//取出匹配正则表达式的内容
			Document src = Jsoup.connect(url)
					.ignoreContentType(true)
					.timeout(10000)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0")
					.get();

			Map<String, String> map = new HashMap<String, String>();

			//获取script标签 然后获取appsrc属性
			String data = src.data();
			int appsrc = data.indexOf("appsrc");
			if (appsrc == -1) {
				continue;
			}

			//根据网址获取视频创建的年月日
			Elements select = src.select("div.u-ptl-c p");

			Pattern p = Pattern.compile("(\\d{4})/(\\d{1,2})/(\\d{1,2})*");
			Matcher m = p.matcher(url);

			if (m.find()) {
				String year = m.group(1);
				String month = m.group(2);
				String daily = m.group(3);
				if (daily == null) {
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
					published = simpleDateFormat.parse(year + "-" + month);
				} else {
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
					published = simpleDateFormat.parse(year + "-" + month + "-" + daily);
				}
			}

			boolean flag = false;
			//获取author colum 和 content
			for (Element element : select) {
				String text = element.text();
				String[] split = text.split("：", 2);
				try {
					map.put(split[0], split[1]);
				} catch (Exception e) {
					logger.error("TEDNEWS格式不匹配");
					flag = true;
					break;
				}
			}

			if (flag) {
				continue;
			}

			//获取视频连接
			String s = data.substring(data.indexOf("appsrc"));
			String http1 = s.substring(s.indexOf("http")).split("',")[0];
			video = http1.substring(0, http1.lastIndexOf(".")) + ".mp4";

			content = map.get("课程简介");

			if (content == null) {
				content = map.get("简介");
			}

			//将内容中的%替换成全角的%
			if (content != null && content.indexOf("%") != -1) {
				content = content.replaceAll("%", "％");
			}

			author = map.get("讲师");
			column = map.get("类型");

			HMessagePlat hMessagePlat = new HMessagePlat();

			hMessagePlat.setMediaSourceUrl(url);
			hMessagePlat.setMessageContent(content);


			hMessagePlat.setPublishSub(column);
			picture = ReptileUtil.upload(picture);
			if (picture == null) {
				continue; //如果上传图片失败,直接跳到最外层循环.不要这篇文章了
			}
			hMessagePlat.setMessageImg(picture);
			hMessagePlat.setMessageTitle(title);
			hMessagePlat.setPublishTime(published);
			hMessagePlat.setAuthor(author);
			hMessagePlat.setMessageVideo(video);
			hMessagePlat.setPlatNo(hConfig.getPlatNo());
			hMessagePlat.setMediaSource(mediaSource);

			hMessagePlats.add(hMessagePlat);
		}
	}

	//获取首页HTMl
	private static JSONArray getTedIndexHtml(String site) throws Exception {
		String htmlData1 = ReptileUtil.getHtmlData(site);

		String substring = htmlData1.substring(htmlData1.indexOf("(") + 1, htmlData1.indexOf(")"));

		JSONObject jsonObject = JSONObject.parseObject(substring);
		return jsonObject.getJSONArray("result");
	}




	//获取网易内容
	private static void getWangYiContent(DealReptileDao dealReptileDao, HConfig hConfig, Date published, String mediaSource, JSONArray result, List<HMessagePlat> hMessagePlats) throws Exception {
		String picture;
		String title;
		String video;
		String content;
		String author;
		String column;
		for (Object o : result) {
			//当条数超过2条执行插入
			if (hMessagePlats.size() >= 2) {
				dealReptileDao.insertMessage(hMessagePlats);
				return;
			}

			JSONObject o1 = (JSONObject) o;
			JSONObject iteminfo = o1.getJSONObject("iteminfo");
			picture = iteminfo.getString("imgurl"); //获取封面图片
			title = iteminfo.getString("title");//获取标题

			//判断是否含有TED 是的话跳过
			if (title.contains("TED")) {
				continue;
			}

			//获取关键字
			String keyword = hConfig.getKeyword();
			//判断关键字是否为null不为null默认全爬取为null的话值获取带有关键字的title文章
			if (keyword != null && !"".equals(keyword.trim())) {
				if (!title.contains(keyword)) {
					continue;
				}
			}

			String url = iteminfo.getString("url");
			System.out.println(picture + ":===" + title + ":===" + url);

			//取出匹配正则表达式的内容
			Document src = Jsoup.connect(url)
					.ignoreContentType(true)
					.timeout(10000)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0")
					.get();

			Map<String, String> map = new HashMap<String, String>();

			//获取script标签 然后获取appsrc属性
			String data = src.data();
			int appsrc = data.indexOf("appsrc");
			if (appsrc == -1) {
				continue;
			}

			//根据网址获取视频创建的年月日
			Elements select = src.select("div.u-ptl-c p");

			Pattern p = Pattern.compile("(\\d{4})/(\\d{1,2})/(\\d{1,2})*");
			Matcher m = p.matcher(url);

			if (m.find()) {
				String year = m.group(1);
				String month = m.group(2);
				String daily = m.group(3);
				if (daily == null) {
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
					published = simpleDateFormat.parse(year + "-" + month);
				} else {
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
					published = simpleDateFormat.parse(year + "-" + month + "-" + daily);
				}
			}

			boolean flag = false;
			//获取author colum 和 content
			for (Element element : select) {
				String text = element.text();
				String[] split = text.split("：", 2);
				try {
					map.put(split[0], split[1]);
				} catch (Exception e) {
					logger.error("网易公开课格式不匹配");
					flag = true;
					break;
				}
			}

			if (flag) {
				continue;
			}

			//获取视频连接
			String s = data.substring(data.indexOf("appsrc"));
			String http1 = s.substring(s.indexOf("http")).split("',")[0];
			video = http1.substring(0, http1.lastIndexOf(".")) + ".mp4";

			content = map.get("课程简介");

			if (content == null) {
				content = map.get("简介");
			}

			//将内容中的%替换成全角的%
			if (content != null && content.indexOf("%") != -1) {
				content = content.replaceAll("%", "％");
			}

			author = map.get("讲师");
			column = map.get("类型");

			HMessagePlat hMessagePlat = new HMessagePlat();

			hMessagePlat.setMediaSourceUrl(url);
			hMessagePlat.setMessageContent(content);


			hMessagePlat.setPublishSub(column);
			picture = ReptileUtil.upload(picture);
			if (picture == null) {
				continue; //如果上传图片失败,直接跳到最外层循环.不要这篇文章了
			}
			hMessagePlat.setMessageImg(picture);
			hMessagePlat.setMessageTitle(title);
			hMessagePlat.setPublishTime(published);
			hMessagePlat.setAuthor(author);
			hMessagePlat.setMessageVideo(video);
			hMessagePlat.setPlatNo(hConfig.getPlatNo());
			hMessagePlat.setMediaSource(mediaSource);

			hMessagePlats.add(hMessagePlat);
		}
	}




	//获取36氪文章内容
	private static void getThirtySixContent(DealReptileDao dealReptileDao, HConfig hConfig, String site, String secondSite, String platNo, String mediaSource, long current, long zero, long ten) throws Exception {
		String published;
		String title;
		String picture;
		String content;
		String column;
		String author;//首先获取36氪所有信息
		String message = ReptileUtil.getHtmlData(site);
		JSONObject jsonObject = JSONObject.parseObject(message); //将信息转成jsonObject


		if (jsonObject.getInteger("code") == 0) { //判断是否成功
			JSONObject data = jsonObject.getJSONObject("data"); //获取json中的数据
			JSONArray items = data.getJSONArray("items");

			//外层循环标识
			//outer:
			for (Object obj : items) { //开始遍历
				JSONObject objdata = (JSONObject) obj;
				published = objdata.getString("published_at");

				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(published));

				if (current > ten) {
					if (ten > calendar.getTimeInMillis()) { //只获取今天10点以后的数据
						return;
					}
				} else {
					if (zero > calendar.getTimeInMillis()) {  //只获取今天0点以后的数据
						return;
					}
				}

				String id = objdata.getString("id"); //拿到文章id后继续发送请求获取文章

				title = objdata.getString("title");

				//获取关键字
				String keyword = hConfig.getKeyword();
				//判断关键字是否为null 为null默认全爬取,不为null的话获取带有关键字的title文章
				if (keyword != null && !"".equals(keyword.trim())) {
					if (!title.contains(keyword)) {
						continue;
					}
				}

				picture = objdata.getString("cover");

				picture = ReptileUtil.upload(picture);

				if (picture == null) {
					continue;
				}

				String htmlData = ReptileUtil.getHtmlData(secondSite + id);

				JSONObject detial = JSONObject.parseObject(htmlData);
				//解析数据
				JSONObject data1 = detial.getJSONObject("data");

				content = data1.getString("content");
				//获取文章栏目集合
				column = data1.getString("extraction_tags");
				//获取作者
				author = data1.getJSONObject("user").getString("name");

				//获取文章栏目
				Pattern p_img1 = Pattern.compile("\"([^\"]*)\"");
				Matcher m_img1 = p_img1.matcher(column);
				if (m_img1.find()) {
					column = m_img1.group(1);
					try {
						column = ReptileUtil.decodeUnicode(column);
					} catch (Exception e) {
						logger.error("解码异常,跳过此篇文章");
						continue;
					}
				}

				//解码文章内容
				content = ReptileUtil.encode(content);

				//排除36氪的推荐内容
				Element body = Jsoup.parse(content).body();
				Elements p = body.getElementsByTag("p");
				for (int i = 0; i < p.size(); i++) {
					if (p.get(i).toString().contains("detect-string")) {
						p.remove(i);
						i--;
					}
				}

				boolean flag = false;

				//获取文章内容中的content,解析img标签.把src提取出来上传到FastDFS
				String s = p.toString();
				Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
				Matcher m_img = p_img.matcher(s);

				boolean result = m_img.find();
				if (result) {
					while (result) {
						String str_img = m_img.group(2);
						//开始匹配<img />标签中的src
						Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
						Matcher m_src = p_src.matcher(str_img);
						if (m_src.find()) {
							String str_src = m_src.group(3);
							str_src = ReptileUtil.upload(hyzUrl, str_src);
							if (str_src == null) {
								flag = true;
								break; //如果上传图片失败,直接跳到最外层循环.不要这篇文章了
							}
							str_src = "img前图片标签" + str_src + "img后图片标签";
							s = s.replaceFirst("<(img|IMG)(.*?)(/>|></img>|>)", str_src);
						}
						result = m_img.find();
					}
				}

				if (flag) {
					continue;
				}

				Document parse = Jsoup.parse(s);
				String s1 = parse.toString();
				String startP = s1.replaceAll("<p", "p前标签<p");
				String all = startP.replaceAll("</p>", "p后标签</p>");
				content = Jsoup.parse(all).text().replaceAll("p前标签", "<p>")
						.replaceAll("p后标签", "</p>")
						.replaceAll("img前图片标签", "<img src='")
						.replaceAll("img后图片标签", "'/>");

				//将内容中的%替换成全角的%
				if (content != null && content.indexOf("%") != -1) {
					content = content.replaceAll("%", "％");
				}

				HMessagePlat hMessagePlat = new HMessagePlat();

				String p1 = secondSite.replaceAll("api/post", "p");
				hMessagePlat.setMediaSourceUrl(p1 + id + ".html");
				hMessagePlat.setAuthor(author);
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				hMessagePlat.setPublishTime(simpleDateFormat.parse(published));

				hMessagePlat.setMediaSource(mediaSource);
				hMessagePlat.setPublishSub(column);
				hMessagePlat.setMessageImg(picture);
				hMessagePlat.setMessageContent(content);

				//将内容中的%替换成全角的%
				if (title != null && title.indexOf("%") != -1) {
					title = title.replaceAll("%", "％");
				}

				hMessagePlat.setMessageTitle(title);
				hMessagePlat.setPlatNo(platNo);
				dealReptileDao.insertWeChatMessage(hMessagePlat);
			}
		}
	}



	//获取澎湃新闻内容
	private static void getPaperContent(DealReptileDao dealReptileDao, HConfig hConfig, String site, String secondSite, String platNo, String mediaSource, String column, long current, long zero, long ten, int index) throws Exception {
		String picture;
		String title;
		String author;
		String content;
		while (true) {
			logger.info("开始拉取澎湃首页数据");
			String homePage = ReptileUtil.getHtmlData(site + index);
			index++;

			logger.info("澎湃首页数据:" + homePage);

			Document document = Jsoup.parse(homePage);

			//获取首页所有文章内容
			Elements newsLi = document.select("div.news_li");

			//外层循环标识
			//outer:
			//遍历每个文章获取对应的id
			for (Element element : newsLi) {
				//解析数据
				Elements tiptitleImgs = element.select("a.tiptitleImg");
				Elements pdttTrbs = element.select("div.pdtt_trbs");
				if (pdttTrbs != null && pdttTrbs.size() > 0) {
					//获取栏目
					Element firstPdttTrbs = pdttTrbs.get(0);
					column = firstPdttTrbs.child(0).text();
				}

				if (tiptitleImgs != null && tiptitleImgs.size() > 0) {
					//获取每个文章href
					Element firstTiptitleImgs = tiptitleImgs.get(0);
					Element child = firstTiptitleImgs.child(0);
					String href = firstTiptitleImgs.attr("href");
					//获取封面图
					picture = child.attr("src");
					picture = "http:" + picture;
					//获取标题
					title = child.attr("alt");

					//获取关键字
					String keyword = hConfig.getKeyword();
					//判断关键字是否为null 为null默认全爬取,不为null的话获取带有关键字的title文章
					if (keyword != null && !"".equals(keyword.trim())) {
						if (!title.contains(keyword)) {
							continue;
						}
					}

					logger.info("澎湃文章标题:" + title + ",配图" + picture + ",文章id:" + href);

					//根据每个id再去获取文章详情内容
					logger.info("开始拉取澎湃文章数据");

					//获取文章详情html
					String htmlData1 = ReptileUtil.getHtmlData(secondSite + href);
					//解析html
					Document parse = Jsoup.parse(htmlData1);
					Elements select1 = parse.select("div.news_about");
					if (select1 != null && select1.size() > 0) {
						Element data = select1.get(0);
						Element Eauthor = data.child(0);
						author = Eauthor.text();
						Element timeAndOrigin = data.child(1);
						String text = timeAndOrigin.text();
						String time = text.substring(0, 16);


						Calendar calendar = Calendar.getInstance();
						calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time));

						//如果当前时间大于10点获取10点以后的数据,否则获取今天0点以后的数据
						if (current > ten) {
							if (ten > calendar.getTimeInMillis()) { //只获取今天10点以后的数据
								return;
							}
						} else {
							if (zero > calendar.getTimeInMillis()) {  //只获取今天0点以后的数据
								return;
							}
						}

						boolean flag = false;

						//获取文章内容并把图片上传到FastDFS
						Elements Econtent = parse.select("div.news_txt");
						if (Econtent != null && Econtent.size() > 0) {
							Element element1 = Econtent.get(0);
							String s = element1.toString();

							Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
							Matcher m_img = p_img.matcher(s);
							boolean result = m_img.find();
							if (result) {
								while (result) {
									String str_img = m_img.group(2);
									//开始匹配<img />标签中的src
									Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
									Matcher m_src = p_src.matcher(str_img);
									if (m_src.find()) {
										String str_src = m_src.group(3);
										str_src = ReptileUtil.upload(hyzUrl, str_src);
										if (str_src == null) {
											flag = true;
											break; //如果上传图片失败,直接跳到最外层循环.不要这篇文章了
										}
										str_src = "img前图片标签" + str_src + "img后图片标签";
										s = s.replaceFirst("<(img|IMG)(.*?)(/>|></img>|>)", str_src);
									}
									result = m_img.find();
								}
							}

							if (flag) {
								continue;
							}

							Document contentParse = Jsoup.parse(s);
							String s1 = contentParse.toString();
							String all = s1.replaceAll("<br>", "br标签<br>");
							content = Jsoup.parse(all).text().replaceAll("br标签", "<br><br>")
									.replaceAll("img前图片标签", "<img src='")
									.replaceAll("img后图片标签", "'/>");

							if (content != null && content.indexOf("%") != -1) {
								content = content.replaceAll("%", "％");
							}

							//上传图片到FastDFS
							picture = ReptileUtil.upload(picture);

							if (picture == null) {
								continue;
							}

							HMessagePlat hMessagePlat = new HMessagePlat();

							hMessagePlat.setMediaSourceUrl(secondSite + href);
							hMessagePlat.setAuthor(author);
							SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
							hMessagePlat.setPublishTime(simpleDateFormat.parse(time));

							hMessagePlat.setPlatNo(platNo);
							hMessagePlat.setMediaSource(mediaSource);
							hMessagePlat.setPublishSub(column);
							hMessagePlat.setMessageImg(picture);
							hMessagePlat.setMessageContent(content);

							//将内容中的%替换成全角的%
							if (title != null && title.indexOf("%") != -1) {
								title = title.replaceAll("%", "％");
							}

							hMessagePlat.setMessageTitle(title);
							dealReptileDao.insertWeChatMessage(hMessagePlat);
						}
					}
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {

//		String htmlData = ReptileUtil.getHtmlDataToday("https://www.toutiao.com/api/pc/feed/?max_behot_time=1515115785&category=__all__&utm_source=toutiao&widen=1&tadrequire=true&as=A1F51AC4BE8DFD1&cp=5A4EDDBF7DE17E1&_signature=vgFBFhAe5F0T6x-XI3bT5r4BQQ");
//		System.out.println(htmlData);
	}
}
